package com.huiminpay.merchant.service.impl;

import com.aliyun.oss.OSS;
import com.aliyun.oss.model.PutObjectRequest;
import com.huiminpay.merchant.service.FileService;
import lombok.SneakyThrows;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.util.UUID;

/**
 * @ClassName FileServiceImpl
 * @Description:
 * @Author: longlong
 * @CreateDate: 2023/12/5 11:38
 * @Version: 1.0
 */


@Service
public class FileServiceImpl implements FileService {
    @Autowired
    OSS oss;
    @Value("${oss.aliyun.bucket}")
    private String bucket;
    @Value("${oss.aliyun.domain}")
    private String domain;

    @SneakyThrows
    @Override
    public String upload(MultipartFile file) {
        if (file == null) {
            return null;
        }
        String fileName = UUID.randomUUID().toString().replace("-", "");
        String originalFilename = file.getOriginalFilename(); // 上传文件原文件名
        String ext = originalFilename.substring(originalFilename.indexOf(".")); // 截取获取扩展名
        PutObjectRequest putObjectRequest =
                new PutObjectRequest(bucket, fileName + ext, file.getInputStream());
        oss.putObject(putObjectRequest);
        return domain + fileName + ext;
    }
}
